home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 2).iso / 1133 / primfunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-17  |  7.0 KB  |  399 lines

  1. /*
  2. **
  3. **  File:           PRIMFUNC.C
  4. **  Description:    Contains general purpose primitive routines
  5. **  Platform:       Windows
  6. **
  7. **
  8. */
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14. #include <string.h>
  15. #include <stdarg.h>
  16. #include <stdlib.h>
  17. #include "nndefs.h"
  18.  
  19.     static char msg1[] = "Bad dimensions in alloc_2d_floats()\n";
  20.     static char msg2[] = "Can't alloc storage for dimension 1 in alloc_2d_floats()\n";
  21.     static char msg3[] = "Can't alloc storage for dimension 2 in alloc_2d_floats()\n";
  22.     static char msg4[] = "Bad dimensions in free_2d_float()\n";
  23.     void Logit(LPSTR fmt, ...);
  24.  
  25.     union {
  26.         float f;
  27.         long l;
  28.         } MissingUnion;
  29. #define MISSING (MissingUnion.f)
  30.  
  31. /*
  32. ** fgetstr
  33. **
  34. ** This function is called to read in a string from an open file. Any ASCII character
  35. ** less than or equal to a blank will terminal the string.
  36. ** 
  37. **
  38. ** Arguments:
  39. **
  40. **      FILE *fd    A pointer to a open file
  41. **      LPSTR str    A pointer to a buffer to put the read in string
  42. **
  43. ** Returns:
  44. **
  45. **      int            returns a zero if there were no errors
  46. */
  47.  
  48. int fgetstr (FILE *fd, LPSTR str){
  49.     int gs;
  50.     LPSTR ps = str;
  51.     gs = fgetc(fd);
  52.     while (gs < 33) {
  53.         if (gs<0) return gs;
  54.         gs = fgetc(fd);
  55.     }
  56.     while (gs > 32) {
  57.         *str=gs;
  58.         str++;
  59.         gs = fgetc(fd);
  60.     }
  61.     *str=0;
  62.     if (gs < 0) return gs;
  63.     else return 0;
  64. }
  65.  
  66. /*
  67. ** fgetfloat
  68. **
  69. ** This function is called to read in a floating point number from an open file. 
  70. ** 
  71. **
  72. ** Arguments:
  73. **
  74. **      FILE *fd    A pointer to a open file
  75. **
  76. ** Returns:
  77. **
  78. **      float        returns the floating point number read in
  79. */
  80.  
  81. float fgetfloat (FILE *fd)
  82. {
  83.     static char cdummy[32];
  84.     float f;
  85.     fgetstr(fd,cdummy);
  86.     f=(float)atof(cdummy);
  87.     return f;
  88. }
  89.  
  90. /*
  91. ** fgetint
  92. **
  93. ** This function is called to read in a integer number from an open file. 
  94. ** 
  95. **
  96. ** Arguments:
  97. **
  98. **      FILE *fd    A pointer to a open file
  99. **
  100. ** Returns:
  101. **
  102. **      int        returns the integer number read in
  103. */
  104.  
  105. int fgetint (FILE *fd)
  106. {
  107.     static char cdummy[32];
  108.     int i;
  109.     fgetstr(fd,cdummy);
  110.     i=atoi(cdummy);
  111.     return i;
  112. }
  113.  
  114. /*
  115. ** fgetlong
  116. **
  117. ** This function is called to read in a long integer number from an open file. 
  118. ** 
  119. **
  120. ** Arguments:
  121. **
  122. **      FILE *fd    A pointer to a open file
  123. **
  124. ** Returns:
  125. **
  126. **      long        returns the long integer number read in
  127. */
  128.  
  129. long fgetlong (FILE *fd)
  130. {
  131.     static char cdummy[32];
  132.     long l;
  133.     fgetstr(fd,cdummy);
  134.     l=atol(cdummy);
  135.     return l;
  136. }
  137.  
  138. /*
  139. ** alloc_2d_floats
  140. **
  141. ** This function is called to create an 2 dimensional array of floating point numbers
  142. ** 
  143. **
  144. ** Arguments:
  145. **
  146. **      int hi1d    The number of columns of numbers
  147. **      int hi2d    The number of rows of numbers
  148. **
  149. ** Returns:
  150. **
  151. **      float **    returns a pointer to the array of 
  152. */
  153.  
  154. float  **alloc_2d_floats(int hi1d, int hi2d )
  155. {
  156.     int     i;
  157.     float **m;
  158.     int len;
  159.     /* check dimensions first */
  160.     if( hi1d<1 || hi2d<1 ) {
  161.         return NULL;
  162.     }
  163.     
  164.     m = (float**) malloc (sizeof(float*) * hi1d);
  165.     if( m == NULL ) {
  166.         return NULL;
  167.     }                  
  168.     len = hi2d*sizeof(float);
  169.     for( i=0; i<hi1d; ++i ) {
  170.         m[i] = (float *) malloc (sizeof(float)*hi2d) ;
  171.         if( m[i] == NULL ) {
  172.             for( i--; i> 0; --i ) free (m[i]);
  173.             free (m);
  174.             return NULL;
  175.         }
  176.         memset (m[i],0,len);
  177.     }
  178.     return m;
  179.  
  180. } // end alloc_2d_floats 
  181.  
  182. /*
  183. ** free_2d_floats
  184. **
  185. ** This function is called to destroy the 2d array of floating point numbers
  186. ** 
  187. **
  188. ** Arguments:
  189. **
  190. **      int **matrix    The pointer to the 2D array
  191. **      int hi1d        The number of columns of numbers
  192. **
  193. ** Returns:
  194. **
  195. **      Nothing
  196. */
  197.  
  198. void free_2d_floats( float **matrix, int hi1d)
  199. {
  200.     int i;
  201.  
  202.     // check dimensions first 
  203.     if( hi1d<1 ) {
  204.         return;
  205.     }
  206.     if( matrix == NULL ) return;
  207.     for( i=0; i<hi1d; i++ ) free (matrix[i]);
  208.     free (matrix);
  209.  
  210. } // end free_2d_floats 
  211.  
  212. /*
  213. ** ToUpper
  214. **
  215. ** This function is called to convert a string to upper case
  216. ** 
  217. **
  218. ** Arguments:
  219. **
  220. **      char *s        A pointer to the character string
  221. **
  222. ** Returns:
  223. **
  224. **      Nothing
  225. */
  226.  
  227. void ToUpper (char *s) {
  228.     strupr(s);
  229. }
  230.  
  231. /*
  232. ** isstring
  233. **
  234. ** This function is called to check to see if the field is a string that can be
  235. ** converted to a number 
  236. ** a number is '0123456789+-eE' but not '.'                      
  237. ** 
  238. **
  239. ** Arguments:
  240. **
  241. **      char *s        A pointer to the character string
  242. **
  243. ** Returns:
  244. **
  245. **      int            returns a 1 if the string is not a number
  246. */
  247.  
  248. int isstring (const char *s) {
  249.     while (*s!=0) {
  250.         if ((*s>='A') && ((*s != 'e') || (*s != 'E'))) return 1;
  251.         s++;
  252.     }
  253.     return 0;
  254. }
  255.  
  256. /*
  257. ** ChkClip
  258. **
  259. ** This function is called to check to see if the number is to be clipped
  260. ** 
  261. **
  262. ** Arguments:
  263. **
  264. **      float f        The value to be cliped
  265. **      float hi    The high clip value
  266. **      float lo    The low clip value
  267. **
  268. ** Returns:
  269. **
  270. **      int            returns a 1 if the number is to be clipped
  271. */
  272.             
  273. int ChkClip (float f, float hi, float lo)
  274. {
  275.     if (f==MISSING) return 1;
  276.     if ((hi!=MISSING) && (f > hi)) return 1;
  277.     if ((lo!=MISSING) && (f < lo)) return 1;
  278.     return 0;
  279. }
  280.  
  281. /*
  282. ** dump
  283. **
  284. ** This function is called to dump a buffer to the log file in hex format
  285. ** 
  286. **
  287. ** Arguments:
  288. **
  289. **      char *buf    A pointer to the buffer to be dumped
  290. **      int count    The number of bytes in the buffer to be dumped
  291. **
  292. ** Returns:
  293. **
  294. **      Nothing
  295. */
  296.             
  297. void dump (const char *buf,int count)
  298. {
  299.     int i;
  300.     static char obuf[128];
  301.     char tbuf[12];
  302.     if (count==0) count=strlen(buf);
  303.  
  304.     obuf[0]=0;
  305.     for (i=0; i < count; i++) {
  306.         if ((i % 16) == 0) {
  307.             strcat (obuf,"\n");
  308.             Logit (obuf);
  309.             sprintf (obuf,"%04x - ",i);
  310.         } 
  311.         sprintf (tbuf,"%02x ",(buf[i]&0xff));
  312.         strcat (obuf,tbuf);
  313.     }
  314.     strcat (obuf,"\n");
  315.     Logit (obuf);
  316. }
  317.  
  318. /*
  319. ** removeleadingblanks
  320. **
  321. ** This function is called to strip a buffer of leading blanks
  322. ** 
  323. **
  324. ** Arguments:
  325. **
  326. **      char *buf    A pointer to the buffer to be striped
  327. **
  328. ** Returns:
  329. **
  330. **      Nothing
  331. */
  332.             
  333. void removeleadingblanks (char *s) {
  334.     char *p = s;
  335.     while (*p==32) p++;
  336.     strcpy (s,p);
  337. }
  338.  
  339. /*
  340. **  fixfieldsize
  341. **
  342. ** This function is called to blank pad a buffer to a fixed size
  343. ** 
  344. **
  345. ** Arguments:
  346. **
  347. **      char *buf    A pointer to the buffer to be padded
  348. **
  349. ** Returns:
  350. **
  351. **      Nothing
  352. */
  353.             
  354. void fixfieldsize(char *s)
  355. {
  356.     int i;
  357.     int l = strlen(s);
  358.     if (l >= MAXSTRING) {
  359.         s[MAXSTRING]=0;
  360.         s[MAXSTRING-1]=32;
  361.         return;
  362.     }
  363.     for (i=l; i < MAXSTRING; i++) s[i] = 32;
  364.     s[MAXSTRING]=0;
  365. }
  366.  
  367. /*
  368. ** Logit
  369. **
  370. ** This function is called to print a string to the log file. Each record is appended to
  371. ** the end of the log file.
  372. ** 
  373. **
  374. ** Arguments:
  375. **
  376. **      char *fmt    A pointer to the format string (same as the printf function)
  377. **
  378. ** Returns:
  379. **
  380. **      Nothing
  381. */
  382.             
  383. void Logit(LPSTR fmt, ...)
  384. {
  385.     FILE *fd;
  386.     char buf[512];
  387.     extern int SessionLog;
  388.     int ret;
  389.     
  390.     va_list args;
  391.     va_start(args,fmt);
  392.     ret = vsprintf(buf,fmt,args);
  393.     fd = fopen("c:\\nncalc.log","a+");
  394.     fputs (buf,fd);
  395.     fclose(fd);
  396.     va_end(args);
  397. }
  398.  
  399.